<!DOCTYPE html>
<html lang="en">
<head>
    <title>AGV调度演示</title>
	<meta charset="utf-8">
	<meta http-equiv="content-type" content="text/html;utf-8" />
	<meta name="keywords" content="agv" />
	<meta name="description" content="AGV" />
	<meta name="author" content="ttscm.com.cn" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no" />
	<script src="/srds/js/jquery-3.6.0.min.js"></script>

	<script>
		var statusInterval = null;
		var signalInterval = null;
		var isAgvCompleted = false;
		//const AGV_PARKING_LOCATION_MARK = "LM15";
		//const TASK_LIST_NAME_01 = "TL_ZFLKM_01";
		//const TASK_LIST_NAME_02 = "TL_ZFLKM_02";
		const AGV_PARKING_LOCATION_MARK = "LM6";
		const TASK_LIST_NAME_01 = "yanshi";
		const TASK_LIST_NAME_02 = "yanshi";

		$(function() {

			//智飞龙科马
			$("#btnZflkm").click(function() {
				execTask();
			});
			
			$("#btnZflkmSet").click(function() {
				setSignal();
			});

			$("#btnZflkmRead").click(function() {
				readSignal2();
			});
			
			$("#btnZflkmReset").click(function() {
				resetSignal2();
			});
			
		});


		function readSignal2() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在读取信号...");
			let strUrl = "/srds/zflkm/readSignalS7/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("已读取值："  + result.data);
				
			});
		}
		
		function resetSignal2() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在重置信号...");
			let strUrl = "/srds/zflkm/resetSignalS7/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("重置信号正常...");
			});
		}
		
		// 设置信号
		function setSignal() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在发送信号...");
			let strUrl = "/srds/zflkm/setSignalS7/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("发送信号正常...");
			});
		}
		
		function execTask() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在下发调度指令...");
			let strUrl = "/srds/zflkm/execTask/" + TASK_LIST_NAME_01;
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("调度指令下发正常...");
				
				statusInterval = setInterval(queryStatus, 1000);
			});
		}

		// 状态查询
		function queryStatus() {
			console.info("--queryStatus...");
			readTaskStatus();
		}
		
		function readTaskStatus() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("任务执行中..." + new Date().getTime());
			
			$.getJSON("/srds/zflkm/location", function(result) {
				if (result.code != 0) {
					clearInterval(statusInterval);
					console.error(result);
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#txtCurrSite").val(result.data);
			});

			$.getJSON("/srds/zflkm/taskStatus", function(result) {
				if (result.code != 0) {
					clearInterval(statusInterval);
					console.error(result);
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				if (result.data.statusCode == 4 && result.data.targetId == AGV_PARKING_LOCATION_MARK) {
					clearInterval(statusInterval);
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("导航任务已完成,呼叫线边设备...");
					console.info("---导航任务已完成，呼叫线边设备...");
					setSignal();
					console.info("---信号已发送，等待设备完成...");
					
					signalInterval = setInterval(querySignal, 5000);
				}
			});
		}
		
		// 查询信号
		function querySignal() {
			console.info("--querySignal...", );
			readSignal();
		}

		// 读取信号
		function readSignal() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在读取信号...");
			let strUrl = "/srds/zflkm/readSignalS7/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					clearInterval(signalInterval);
					return;
				}
				let iSignal = result.data;
				if (iSignal == 1) {
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("等待线边设备执行...");
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("已读取值："  + iSignal);
				if (iSignal == 2) {//线边设备完成动作后置信号为2
					console.info("---蛋车已空...");
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("蛋车已空...");
				
					clearInterval(signalInterval);
					
					console.info("---重置设备信号...");
					$("#areaExecuteStatus").css("color", "#000000");
					$("#areaExecuteStatus").text("重置设备信号...");
					resetSignal();
				}
			});
		}


		// 重置信号
		function resetSignal() {
			$("#areaExecuteStatus").css("color", "#000000");
			$("#areaExecuteStatus").text("正在重置信号...");
			let strUrl = "/srds/zflkm/resetSignalS7/";
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("重置信号正常...");
				
				console.info("---呼叫机器人回程...");
				$("#areaExecuteStatus").css("color", "#000000");
				$("#areaExecuteStatus").text("呼叫机器人回程...");
				execTaskBack();
			});
		}
		
		function execTaskBack() {
		//	$("#areaExecuteStatus").css("color", "#000000");
		//	$("#areaExecuteStatus").text("正在下发调度指令...");
			let strUrl = "/srds/zflkm/execTask/" + TASK_LIST_NAME_02;
			$.getJSON(strUrl, function(result) {
				if (result.code != 0) {
					$("#areaExecuteStatus").css("color", "#FF0000");
					$("#areaExecuteStatus").text("异常代码：" + result.code + "\n\n异常消息：" + result.message);
					return;
				}
		//		$("#areaExecuteStatus").css("color", "#000000");
		//		$("#areaExecuteStatus").text("调度指令下发正常...");
			});
		}
  </script>
</head>
<body style="font-size:100%;">
	<div id="container" style="border:0px solid #FF0000;margin:0px;padding:1px;">
		<div id="header" style="border:0px solid #00FF00;background-color:#C0C0C0;text-align:center;font-size:200%;margin:5px;padding:1px;">
			<b>调度演示</b>
		</div>

		<div id="content" style="border:0px solid #0000FF;background-color:#EEEEEE;margin:5px;padding:1px;">	
			<table style="width:100%;">
				<tr>
					<td style="width:30%;"><label for="txtCurrSite">当前站点：</label></td>
					<td style="width:70%;"><input id="txtCurrSite" type="text" style="width:95%;height:25px;" disabled="disabled" /></td>
				</tr>
				<tr>
					<td colspan="2" style="text-align:center;height:80px;">
						<button id="btnZflkm" type="button" style="width:100%;height:50px;background-color:red;">定点-上料-卸料</button>
						<!---->
						<button id="btnZflkmSet" type="button" style="width:100%;height:50px;background-color:red;">发送信号</button>
						<button id="btnZflkmRead" type="button" style="width:100%;height:50px;background-color:red;">读取信号</button>
						<button id="btnZflkmReset" type="button" style="width:100%;height:50px;background-color:red;">重置信号</button>

					</td>
				</tr>
				<tr>
					<td>
						<label for="areaExecuteStatus" >执行状态：</label>
					</td>
					<td style="text-align:center;">
						<textarea id="areaExecuteStatus" style="width:95%;min-height:400px;font-size:150;font-weight:bold;color:#000000%" disabled="disabled"></textarea>
					</td>
				</tr>
			</table>
		</div>

		<div id="footer" style="border:0px solid #00FF00;background-color:#C0C0C0;text-align:center;margin:5px;padding:1px;">
			版权 &#169 天天供应链 &#8226 天天智能科技
		</div>
	</div>
</body>
</html>